<template>
  <div class="dev">
    <!-- 搜索框 -->
    <div class="search">
      <el-input placeholder="请输入房东名称" clearable prefix-icon="el-icon-search" v-model="landlord">
      </el-input>
      <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      <el-button type="danger" :disabled="ids.length == 0 ? true : false" icon="el-icon-close"
        @click="isActive(0)">禁用</el-button>
      <el-button type="success" :disabled="ids.length == 0 ? true : false" icon="el-icon-check"
        @click="isActive(1)">启用</el-button>
      <el-button type="primary" :disabled="ids.length == 0 ? true : false" icon="el-icon-data-line"
        @click="charge">设置手续费</el-button>
      <el-button type="warning" :disabled="ids.length == 0 ? true : false" icon="el-icon-magic-stick"
        @click="support(1)">支持切换充值方式</el-button>
      <el-button type="info" :disabled="ids.length == 0 ? true : false" icon="el-icon-magic-stick"
        @click="support(0)">不支持切换充值方式</el-button>
    </div>
    <!-- 表格数据 -->
    <div class="table">
      <el-table :data="tableData" ref="isExpand" v-loading="loading" border height="calc(100%-10rem)"
        highlight-current-row stripe style="width: 100%" @selection-change="handleSelectionChange">
        >
        <template slot="empty">
          <el-empty description="暂无数据"></el-empty>
        </template>
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column align="center" prop="userName" label="房东">
        </el-table-column>
        <el-table-column align="center" prop="elecMeterCount" label="电表数量">
        </el-table-column>
        <el-table-column width="160" align="center" prop="phone" label="手机号码">
        </el-table-column>
        <el-table-column align="center" prop="meterServiceCharge" label="手续费">
          <template slot-scope="scope">
            <el-tag effect="plain">{{ scope.row.meterServiceCharge }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="200" align="center" prop="meterSupportCharge" label="是否支持切换充值方式">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.meterSupportCharge == 1">支持</el-tag>
            <el-tag type="danger" v-if="scope.row.meterSupportCharge == 0">不支持</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="meterOnlineCharge" label="充值方式">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.meterOnlineCharge == 1">线上充值</el-tag>
            <el-tag v-if="scope.row.meterOnlineCharge == 0">线下充值</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="isActive" label="是否禁用">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.isActive == 1">启用</el-tag>
            <el-tag type="danger" v-if="scope.row.isActive == 0">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="180" align="center" prop="createTime" label="注册时间">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="setting(scope.row)">设置</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination v-show="tableData.length !== 0" background @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[5, 10, 15, 20, 25, 30]"
        :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 设置 -->
    <div class="edit">
      <el-dialog :close-on-click-modal="false" title="设置" :visible.sync="editDialogVisible" width="600px">
        <el-form :model="editForm" ref="editForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="是否禁用">
            <el-select v-model="editForm.isActive" placeholder="请选择">
              <el-option value="false" label="禁用"> </el-option>
              <el-option value="true" label="启用"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="手续费">
            <el-select v-model="editForm.chargeItem" placeholder="请选择">
              <el-option v-for="item in chargeList" :value="item.serviceChargeId" :key="item.serviceChargeId"
                :label="item.serviceChargeName">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="切换充值方式">
            <el-select v-model="editForm.isSupport" placeholder="请选择">
              <el-option value="false" label="不支持"> </el-option>
              <el-option value="true" label="支持"> </el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editFn">确 认</el-button>
        </span>
      </el-dialog>
    </div>

    <!-- 设置手续费-->
    <div class="add">
      <el-dialog :close-on-click-modal="false" title="新增" :visible.sync="DialogVisible" width="600px">
        <el-form :model="chargeForm" ref="chargeForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="手续费" prop="chargeItem">
            <el-select v-model="chargeForm.chargeItem" placeholder="请选择">
              <el-option v-for="item in chargeList" :value="item.serviceChargeId" :key="item.serviceChargeId"
                :label="item.serviceChargeName">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="DialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="chargeFn">确 认</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getLandlordApi,
  getChargeApi,
  putActiveApi,
  putChargeApi,
  putSupportApi,
  putActiveMoreApi,
  putSupportMoreApi,
  putChargeMoreApi,
} from "../../../api/system/landlord";
export default {
  data() {
    return {
      landlord: "",
      loading: false,
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: null,
      ids: [],
      chargeList: [],
      editDialogVisible: false,
      DialogVisible: false,
      chargeForm: {
        chargeItem: 1,
        landlordIds: [],
      },
      editForm: {
        isActive: "",
        isSupport: "",
        chargeItem: "",
      },
    };
  },
  created() {
    this.pageSize = Number(sessionStorage.getItem("pageSize")) || 10;
    this.pageNum = Number(sessionStorage.getItem("pageNum")) || 1;
    this.getLandlord(this.pageNum, this.pageSize);
  },
  destroyed() {
    sessionStorage.removeItem("pageNum");
    sessionStorage.removeItem("pageSize");
  },
  methods: {
    // 获取房东列表
    getLandlord(pageNum, pageSize, landlord) {
      getLandlordApi(pageNum, pageSize, landlord).then((res) => {
        this.tableData = res.data.rows;
        this.total = res.data.total;
        console.log(res);
      });
    },
    // 查询
    search() {
      this.getLandlord(this.pageNum, this.pageSize, this.landlord);
    },
    // 分页
    handleSizeChange(val) {
      this.getLandlord(this.pageNum, val);
      sessionStorage.setItem("pageSize", val);
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.getLandlord(val, this.pageSize);
      sessionStorage.setItem("pageNum", val);
      this.pageNum = val;
    },
    // 批量操作
    handleSelectionChange(e) {
      let arr = [];
      e.forEach((item) => {
        arr.push(item.userId);
      });
      this.ids = arr;
    },
    // 打开设置
    setting(row) {
      // console.log(row);
      getChargeApi().then((res) => {
        this.chargeList = res.data;
      });
      if (row.isActive == 1) {
        this.editForm.isActive = "true";
      } else {
        this.editForm.isActive = "false";
      }

      if (row.meterSupportCharge == 1) {
        this.editForm.isSupport = "true";
      } else {
        this.editForm.isSupport = "false";
      }
      this.editId = row.userId;
      this.editForm.chargeItem = row.meterServiceChargeId;
      this.editDialogVisible = true;
    },
    // 修改
    editFn() {
      console.log(this.editForm);
      // 修改是否禁用
      putActiveApi(this.editId, this.editForm.isActive)
        .then((res) => {
          //   修改费率
          putChargeApi(this.editId, this.editForm.chargeItem)
            .then((res) => {
              //   修改是否支持切换充值方式
              putSupportApi(this.editId, this.editForm.isSupport)
                .then((res) => {
                  this.$message.success("操作成功");
                  this.editDialogVisible = false;
                  this.search();
                })
                .catch((err) => {
                  this.$message.error("修改是否支持失败");
                });
            })
            .catch((err) => {
              this.$message.error("修改费率失败");
            });
        })
        .catch((err) => {
          this.$message.error("修改是否禁用失败");
        });
    },
    // 批量禁用or启用
    isActive(e) {
      if (e == 0) {
        console.log("禁用");
        let arr = {
          isActive: false,
          landlordIds: this.ids,
        };
        putActiveMoreApi(arr).then((res) => {
          this.search();
          this.ids = [];
          this.$message.success("禁用成功");
        });
      } else {
        console.log("启用");
        let arr1 = {
          isActive: true,
          landlordIds: this.ids,
        };
        putActiveMoreApi(arr1).then((res) => {
          this.search();
          this.ids = [];
          this.$message.success("启用成功");
        });
      }
    },
    // 打开批量设置手续费
    charge() {
      this.chargeForm.chargeItem = 1;
      this.DialogVisible = true;
      getChargeApi().then((res) => {
        this.chargeList = res.data;
      });
      this.chargeForm.landlordIds = this.ids;
    },
    // 批量设置手续费
    chargeFn() {
      putChargeMoreApi(this.chargeForm)
        .then((res) => {
          this.$message.success("设置手续费成功");
          this.search();
          this.DialogVisible = false;
        })
        .catch((err) => {
          this.$message.error("设置手续费失败！");
        });
    },
    // 批量支持 or 不支持
    support(e) {
      if (e == 0) {
        console.log("不支持");
        let arr = {
          isSupport: false,
          landlordIds: this.ids,
        };
        putSupportMoreApi(arr).then((res) => {
          this.search();
          this.ids = [];
          this.$message.success("操作成功");
        });
      } else {
        console.log("支持");
        let arr1 = {
          isSupport: true,
          landlordIds: this.ids,
        };
        putSupportMoreApi(arr1).then((res) => {
          this.search();
          this.ids = [];
          this.$message.success("操作成功");
        });
      }
    },
  },
};
</script>


<style lang="scss" scoped>
.dev {
  padding-top: 15px;
  padding: 0 15px;
  height: calc(100vh - 100px);

  .search {
    width: 100%;
    margin-bottom: 15px;

    .el-input {
      width: 200px;
    }

    .el-button {
      margin-left: 15px;
    }
  }

  .table {
    height: 100%;

    .el-pagination {
      text-align: center;
      margin-top: 20px;
    }
  }
}
</style>